package com.xbongbong.sso.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.sso.pojo.dto.OauthConnectorAddDTO;
import com.xbongbong.sso.pojo.dto.OauthConnectorDeleteDTO;
import com.xbongbong.sso.pojo.dto.OauthConnectorDetailDTO;
import com.xbongbong.sso.pojo.dto.OauthConnectorEnableDTO;
import com.xbongbong.sso.pojo.dto.OauthConnectorListDTO;
import com.xbongbong.sso.pojo.dto.OauthConnectorUpdateDTO;
import com.xbongbong.sso.pojo.vo.OauthConnectorAddVO;
import com.xbongbong.sso.pojo.vo.OauthConnectorDetailVO;
import com.xbongbong.sso.pojo.vo.OauthConnectorEnableVO;
import com.xbongbong.sso.pojo.vo.OauthConnectorExistVO;
import com.xbongbong.sso.pojo.vo.OauthConnectorListVO;
import com.xbongbong.sso.pojo.vo.OauthConnectorUpdateVO;
import com.xbongbong.sso.service.OauthConnectorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 授权连接器
 * 创建时间 2020/12/28 2:51 PM
 * 修改时间 2020/12/28 2:51 PM
 * @author chy
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/oauthConnector")
public class OauthConnectorController {

    private static final Logger LOG = LoggerFactory.getLogger(OauthConnectorController.class);

    @Resource
    private OauthConnectorService oauthConnectorService;

    /**
     * 授权连接器添加
     * @param baseDTO 关联DTO
     */
    @RequestMapping(value = "/exist", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String exist(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) throws Exception{
        XbbResponse<OauthConnectorExistVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthConnectorExistVO oauthConnectorExistVO = oauthConnectorService.exist(baseDTO);
                response = new XbbResponse<>(oauthConnectorExistVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 授权连接器添加
     * @param oauthConnectorAddDTO 关联DTO
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid OauthConnectorAddDTO oauthConnectorAddDTO, BindingResult br) throws Exception{
        XbbResponse<OauthConnectorAddVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthConnectorAddVO oauthConnectorAddVO = oauthConnectorService.add(oauthConnectorAddDTO);
                response = new XbbResponse<>(oauthConnectorAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 授权连接器编辑
     * @param oauthConnectorUpdateDTO 关联DTO
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid OauthConnectorUpdateDTO oauthConnectorUpdateDTO, BindingResult br) throws Exception{
        XbbResponse<OauthConnectorUpdateVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthConnectorUpdateVO oauthConnectorUpdateVO = oauthConnectorService.update(oauthConnectorUpdateDTO);
                response = new XbbResponse<>(oauthConnectorUpdateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 授权连接器列表
     * @param oauthConnectorListDTO 关联DTO
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid OauthConnectorListDTO oauthConnectorListDTO, BindingResult br) throws Exception{
        XbbResponse<OauthConnectorListVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthConnectorListVO oauthConnectorListVO = oauthConnectorService.list(oauthConnectorListDTO);
                response = new XbbResponse<>(oauthConnectorListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 授权连接器详情
     * @param oauthConnectorDetailDTO 关联DTO
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String detail(@RequestBody @Valid OauthConnectorDetailDTO oauthConnectorDetailDTO, BindingResult br) throws Exception{
        XbbResponse<OauthConnectorDetailVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthConnectorDetailVO oauthConnectorDetailVO = oauthConnectorService.detail(oauthConnectorDetailDTO);
                response = new XbbResponse<>(oauthConnectorDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 授权连接器开关
     * @param oauthConnectorEnableDTO 关联DTO
     */
    @RequestMapping(value = "/enable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String enable(@RequestBody @Valid OauthConnectorEnableDTO oauthConnectorEnableDTO, BindingResult br) throws Exception{
        XbbResponse<OauthConnectorEnableVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthConnectorEnableVO oauthConnectorEnableVO = oauthConnectorService.enable(oauthConnectorEnableDTO);
                response = new XbbResponse<>(oauthConnectorEnableVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 授权连接器删除
     * @param oauthConnectorDeleteDTO 关联DTO
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid OauthConnectorDeleteDTO oauthConnectorDeleteDTO, BindingResult br) throws Exception{
        XbbResponse<BaseVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = oauthConnectorService.delete(oauthConnectorDeleteDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
